#
#   main.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Nicholas F. Hoover
#   Contributors: 
#   Created: 2007.06.16
#   Last Modified: 2007.07.13
#

import pygame
import Messages, EventHandler
from Game import Game
from ResourceManager import ResourceManager
from EventHandler import GameEventHandler

def main():
   m = Main()
   m.go()

class Main(object):

   WINDOW_SIZE = (800, 600)
   WINDOW_TITLE = 'Evil Greg VS. The 8 Year Olds'

   USE_FULLSCREEN = False
   if USE_FULLSCREEN: FULLSCREEN = pygame.FULLSCREEN
   else: FULLSCREEN = 0


   def __init__(self):
      pygame.init()
      pygame.mixer.init()

      #setup up window
      self.screen = pygame.display.set_mode(self.WINDOW_SIZE, Main.FULLSCREEN)
      pygame.display.set_caption(self.WINDOW_TITLE)
      pygame.mouse.set_visible(False)
      
      self.rManager = ResourceManager()
      self.screen.blit(self.rManager.getSplashScreen(),(0,0))
      Messages.printLoading(self.screen)
      pygame.display.flip()
      self.changeSong(ResourceManager.INTRO_SONG)
      
      self.rManager.loadResources()
      Messages.printDoneLoading(self.screen)
      pygame.display.flip()
      EventHandler.waitForKeyOrQuit(EventHandler.ENTER_KEY)
      
      self.highScore = 0

      self.game = Game(self.screen, self.rManager)


   def go(self):
      while True:
         self.game.background.draw()
         self.game.tintScreen()
         Messages.printInstructions(self.screen)
         pygame.display.flip()

                                        
         EventHandler.waitForKeyOrQuit(EventHandler.ENTER_KEY)
         self.changeSong(ResourceManager.GAME_SONG)
         self.game.background.draw()
         self.game.mainLoop()
         self.stopGame()
         self.game.reset()



   def stopGame(self):
      self.game.tintScreen()
      self.changeSong(ResourceManager.DEAD_SONG, 0)
      score = self.game.getScore()
      if score > self.highScore: self.highScore = score

      Messages.printGameOver(self.screen, score, self.highScore)

      pygame.display.flip()
      EventHandler.waitForKeyOrQuit(EventHandler.ENTER_KEY)
      self.changeSong(ResourceManager.INTRO_SONG)

   def changeSong(self, newSong, loop = -1):
      pygame.mixer.music.stop()
      self.rManager.loadSong(newSong)
      pygame.mixer.music.play(loop)
